﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Model;

namespace Common
{
    public class OperateAgreement
    {

        public static string GetTaxiState(int taxiState)
        {
            string str;
            switch (taxiState)
            {
                case 0:
                    str = "空载";
                    break;
                case 1:
                    str = "预约";
                    break;
                case 2:
                    str = "载客";
                    break;
                case 3:
                    str = "交班";
                    break;
                default:
                    str = "可以拼车";
                    break;

            }
            return str;
        }

        public static string GetTaxiStar(int taxiStar)
        {
            string str;                   //☆☆★★
            switch (taxiStar)
            {
                case 1:
                    str = "★☆☆☆☆";
                    break;
                case 2:
                    str = "★★☆☆☆";
                    break;
                case 3:
                    str = "★★★☆☆";
                    break;
                case 4:
                    str = "★★★★☆";
                    break;
                case 5:
                    str = "★★★★★";
                    break;
                default:
                    str = "★★★★★";
                    break;
            }
            return str;
        }


        public static string GetTaxiAlarmCotent(int alarmL)
        {
            string str;
            switch (alarmL)
            {
                case 0:
                    str = "无告警";
                    break;
                case 1:
                    str = "跑出规定区域!";
                    break;
                case 2:
                    str = "被劫持!";
                    break;
                case 3:
                    str = "被劫持+跑出规定区域!";
                    break;
                case 4:
                    str = "震动";
                    break;
                case 5:
                    str = "电量告警";
                    break;
                case 6:
                    str = "超速告警";
                    break;
                case 7:
                    str = "疲劳驾驶";
                    break;
                case 8:
                    str = "超时停车";
                    break;
                default:
                    str = "未知告警";
                    break;

            }
            return str;
        }

        public static string GetTaxiWarmingState(int value)
        {
            string str;
            switch (value)
            {
                case 1:
                    str = "震动告警";
                    break;
                case 2:
                    str = "电量告警";
                    break;
                case 3:
                    str = "超速告警";
                    break;
                case 4:
                    str = "疲劳驾驶告警";
                    break;
                case 5:
                    str = "ACC闭合";
                    break;
                case 6:
                    str = "ACC断开";
                    break;
                case 7:
                    str = "超时停车告警";
                    break;
                case 8:
                    str = "震动告警恢复";
                    break;
                case 9:
                    str = "电量告警恢复";
                    break;
                case 10:
                    str = "超速告警恢复";
                    break;
                case 11:
                    str = "疲劳驾驶告警恢复";
                    break;
                case 12:
                    str = "超时停车告警恢复";
                    break;
                default:
                    str = "未知告警";
                    break;

            }
            return str;
        }


        public static string GetPhoneNumInMessageHead(byte[] messageHead)
        {
            if (messageHead.Length < ConstParameters.MhLenth)
            {
                return "";
            }
            byte[] phoneNumberbyts = new byte[6];
            Array.Copy(messageHead, 5, phoneNumberbyts, 0, 6);
            string phoneNumber = CommonRoutine.ByteToBcdString(phoneNumberbyts);
            return phoneNumber.Remove(0, 1);
        }

        public static string GetPhoneNumInTransferMessageHead(byte[] messageHead)
        {
            if (messageHead.Length < ConstParameters.MhLenth)
            {
                return "";
            }
            byte[] phoneNumberbyts = new byte[6];
            Array.Copy(messageHead, ConstParameters.MBodySatrtIndex + 2, phoneNumberbyts, 0, 6);
            string phoneNumber = CommonRoutine.ByteToBcdString(phoneNumberbyts);
            return phoneNumber.Remove(0, 1);
        }

        public static int GetMessageLengthInMessageHead(byte[] messageHead)
        {
            // receData[3] -- Message body attribute in Message Head
            //----------------------receData[4]------   ---------------receData[3]----------
            //15   14	   13	     12   11  10       9    8    7    6    5    4    3    2    1    0
            //    保留	  分包	数据加密方式	    消息体长度
            string messageBodyAttribute = CommonRoutine.IntToBinaryString(messageHead[4]) + CommonRoutine.IntToBinaryString(messageHead[3]);
            string strRecedataLegth = messageBodyAttribute.Remove(0, 6);
            int recedataLegth = Convert.ToInt32(strRecedataLegth, 2); //Meaage body length
            return recedataLegth;
        }


        public static bool GetIsSubPackageInMessageHead(byte[] messageHead)
        {
            // receData[3] -- Message body attribute in Message Head
            //----------------------receData[4]------   ---------------receData[3]----------
            //15   14	   13	     12   11  10       9    8    7    6    5    4    3    2    1    0
            //    保留	  分包	数据加密方式	    消息体长度
            string messageBodyAttribute = CommonRoutine.IntToBinaryString(messageHead[4])
                + CommonRoutine.IntToBinaryString(messageHead[3]);
            char isSubPackage = messageBodyAttribute[2];
            return isSubPackage != '0';
        }

        public static int GetPackageCountInMessageHead(byte[] messageHead)
        {
            Array.Reverse(messageHead, 13, 2);
            ushort PackageCount = BitConverter.ToUInt16(messageHead, 13);
            return PackageCount;
        }

        public static int GetPackageIndexInMessageHead(byte[] messageHead)
        {
            Array.Reverse(messageHead, 15, 2);
            ushort PackageIndex = BitConverter.ToUInt16(messageHead, 15);
            return PackageIndex;
        }

        public static ushort GetMessageIdInMessageHead(byte[] messageHead)
        {
            Array.Reverse(messageHead, 1, 2);
            ushort messageId = BitConverter.ToUInt16(messageHead, 1);
            //restore
            Array.Reverse(messageHead, 1, 2);
            return messageId;
        }

        public static ushort GetMessageIdInTransferMessageHead(byte[] messageHead)
        {
            Array.Reverse(messageHead, ConstParameters.MBodySatrtIndex, 2);
            ushort realMessageId = BitConverter.ToUInt16(messageHead, ConstParameters.MBodySatrtIndex);
            return realMessageId;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="messageId"></param>
        /// <param name="isSubPackage"></param>
        /// <param name="isRsaEncrypt"></param>
        /// <param name="messageBodyLength">must less than 1023</param>
        /// <param name="phoneNumber">rece the data in meaage</param>
        /// <param name="serialNumber">rece the data in meaage</param>
        /// <param name="packgeCounts">0~65,535</param>
        /// <param name="packageIndex">0~65,535</param>
        /// <returns></returns>
        public static byte[] GetMessageHead(ushort messageId, bool isSubPackage, bool isRsaEncrypt, int messageBodyLength, byte[] phoneNumber, byte[] serialNumber, ushort packgeCounts, ushort packageIndex)
        {
            List<byte> byteList = new List<byte>();
            //MessageId
            byteList.AddRange(BitConverter.GetBytes(messageId).Reverse());
            // receData[3] -- Message body attribute in Message Head
            //----------------------receData[4]------   ---------------receData[3]----------
            //15   14	   13	     12   11  10       9    8    7    6    5    4    3    2    1    0
            //    保留	  分包	数据加密方式	    消息体长度
            StringBuilder messageBodyAttribute = new StringBuilder();
            messageBodyAttribute.Append("00");// 15~14 for KEEP 
            messageBodyAttribute.Append(isSubPackage ? 1 : 0);//13
            messageBodyAttribute.Append("00");// 12~11 for KEEP 
            messageBodyAttribute.Append(isRsaEncrypt ? 1 : 0);//10
            messageBodyAttribute.Append(CommonRoutine.IntToBinaryString(messageBodyLength).PadLeft(10, '0'));//0~9
            string messageBodyAttribute2 = messageBodyAttribute.ToString();

            //string strRecedataLegthqq = messageBodyAttribute2.Remove(0, 6);
            //int recedataLegth = Convert.ToInt32(strRecedataLegthqq, 2); //Meaage body length
            //string messageBodyAttribute2 = CommonRoutine.ReverseString(messageBodyAttribute.ToString()); //ReverseString

            byteList.Add(Convert.ToByte(messageBodyAttribute2.Remove(0, 8), 2));
            byteList.Add(Convert.ToByte(messageBodyAttribute2.Remove(8), 2));
            //phonenumber 12
            byteList.AddRange(phoneNumber);
            //serialNumber 2
            byteList.AddRange(serialNumber);
            //packgeCounts
            byteList.AddRange(BitConverter.GetBytes(packgeCounts).Reverse());
            //packageIndex
            byteList.AddRange(BitConverter.GetBytes(packageIndex).Reverse());
            return byteList.ToArray();
        }



        public static byte[] PackageSendData(byte[] messageHead, byte[] messageBody)
        {
            List<byte> messageData = new List<byte>();
            messageData.Add(0x7e);
            messageData.AddRange(messageHead);
            messageData.AddRange(messageBody);
            messageData.Add(0x00); //temp checkbit;
            messageData.Add(0x7e);
            byte[] messageDatabytes = messageData.ToArray();
            messageDatabytes[messageDatabytes.Length - 2] = CommonRoutine.GetCheckBit(messageDatabytes);
            return TransferToSendData(messageDatabytes);
        }


        /// <summary>
        /// Judge the receive data  is or is  not   completion data
        /// </summary>
        /// <returns></returns>
        public static bool JudgeisCompleteData(byte[] realData)
        {

            if (realData.Length < ConstParameters.MhLenth || realData[0] != 0x7e || realData[realData.Length - 1] != 0x7e)
            {
                //LogService.WriteLog
                Console.WriteLine("realData.Length < ConstParameters.MhLenth || realData[0] != 0x7e || realData[realData.Length - 1] != 0x7e   " +
                   realData.Length.ToString() + "--realData[0] =" + realData[0].ToString("X2") + "- realData[realData.Length - 1]= " + realData[realData.Length - 1].ToString("X2"));
                return false;
            }
            // receData[3] -- Message body attribute in Message Head
            //----------------------receData[4]------   ---------------receData[3]----------
            //15   14	   13	     12   11  10       9    8    7    6    5    4    3    2    1    0
            //    保留	  分包	数据加密方式	    消息体长度
            int recedataLegth = GetMessageLengthInMessageHead(realData);
            if (recedataLegth != realData.Length - ConstParameters.MhLenth)
            {
                Console.WriteLine("recedataLegth != realData.Length - ConstParameters.MhLenth" + recedataLegth.ToString()
                                                 + "--" + realData.Length);

                return false;
            }
            byte checkbit = CommonRoutine.GetCheckBit(realData);
            if (checkbit != realData[realData.Length - 2])
            {
                Console.WriteLine("checkbit != realData[realData.Length - 2]" + checkbit.ToString("X2")
                                                 + "--" + realData[realData.Length - 2].ToString("X2"));
                return false;
            }
            return true;
        }


        /// <summary>
        /// Restore ReceData. 0x7d  0x02 -- 0x7e ；0x7d  0x01  --- 0x7d；
        /// </summary>
        /// <param name="receData">full data </param>
        /// <returns>realData</returns>
        public static byte[] RestoreReceData(byte[] receData)
        {
            List<byte> byteList = new List<byte>();
            for (int i = 0; i < receData.Length; i++)
            {
                if (i != receData.Length - 1)
                {
                    if (receData[i] == 0x7d && receData[i + 1] == 0x01)
                    {
                        byteList.Add(0x7d);
                        i++;
                        continue;
                    }
                    if (receData[i] == 0x7d && receData[i + 1] == 0x02)
                    {
                        byteList.Add(0x7e);
                        i++;
                        continue;
                    }
                }
                byteList.Add(receData[i]);
            }
            return byteList.ToArray();
        }

        /// <summary>
        /// Restore ReceData. 0x7e -- 0x7d  0x02；0x7d --- 0x7d  0x01；
        /// </summary>
        /// <param name="realData">full --data</param>
        /// <returns>realData</returns>
        public static byte[] TransferToSendData(byte[] realData)
        {
            List<byte> byteList = new List<byte>();
            byteList.Add(0x7e);
            //----Take out the first and the last
            for (int i = 1; i < realData.Length - 1; i++)
            {
                if (realData[i] == 0x7e)
                {
                    byteList.Add(0x7d);
                    byteList.Add(0x02);
                    continue;
                }
                if (realData[i] == 0x7d)
                {
                    byteList.Add(0x7d);
                    byteList.Add(0x01);
                    continue;
                }
                byteList.Add(realData[i]);
            }
            byteList.Add(0x7e);
            return byteList.ToArray();
        }


        /// <summary>
        /// message body- 1byte 0
        /// </summary>
        /// <param name="messageId"></param>
        /// <returns></returns>
        public static byte[] GetEmptyBodyMessageData(ushort messageId)
        {
            byte[] mbodydata = new byte[1];
            byte[] mHead = GetMessageHead(messageId, false, false, mbodydata.Length, new byte[6],
                              new byte[2], 0, 0);
            byte[] tempData1 = PackageSendData(mHead, mbodydata);
            return tempData1;
        }

    }
}
